package com.myres.message.actions;
import java.sql.Connection;
import com.myres.db.ConnectionPool;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.myres.model.Message;
import java.util.List;
import java.util.ArrayList;

public class MessageService {
	public  List<Message> getMyMessage(int uid,byte status){
		Connection con=null;
		try{
			con=ConnectionPool.getPool().getConnection();
			PreparedStatement pstmt=con.prepareStatement("select * from res_messages where _receiver=? and _status=? order by _sent_time desc");
			pstmt.setInt(1, uid);
			pstmt.setByte(2, status);
			ResultSet rs=pstmt.executeQuery();
			return Message.populate(rs);
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}finally{
		    try{
		    	con.close();
		    }catch(Exception e){
		    	
		    }
		}
	}
	public List<Message> getThreadDetail(int uid,int sender){
		Connection con=null;
		try{
			con=ConnectionPool.getPool().getConnection();
			PreparedStatement pstmt=con.prepareStatement("select * from res_messages where _receiver=? and _sender=? order by _sent_time desc");
			pstmt.setInt(1, uid);
			pstmt.setInt(2, sender);
			ResultSet rs=pstmt.executeQuery();
			return Message.populate(rs);
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}finally{
		    try{
		    	con.close();
		    }catch(Exception e){
		    	
		    }
		}
	}
	public List<Message> getThreads(int uid){
		Connection con=null;
		try{
			con=ConnectionPool.getPool().getConnection();
			String sql="select temp.* from res_messages as temp  where temp._receiver=? and temp._sent_time= (select Max(b._sent_time) from res_messages as b where b._sender=temp._sender and b._receiver=temp._receiver) order by temp._sent_time desc";
			PreparedStatement pstmt=con.prepareStatement(sql);
			pstmt.setInt(1, uid);
			ResultSet rs=pstmt.executeQuery();
			return Message.populate(rs);
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}finally{
			try{
				con.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
	}
	public  List<Message> getMyMessage(int uid){
		Connection con=null;
		try{
			con=ConnectionPool.getPool().getConnection();
			PreparedStatement pstmt=con.prepareStatement("select * from res_messages where _receiver=? order by _sent_time desc");
			pstmt.setInt(1, uid);
			ResultSet rs=pstmt.executeQuery();
			return Message.populate(rs);
		}catch(SQLException e){
			e.printStackTrace();
			return null;
		}finally{
		    try{
		    	con.close();
		    }catch(Exception e){
		    	
		    }
		}
	}
	public int getNewMessageCount(int uid){
		Connection con=null;
		try{
			con=ConnectionPool.getPool().getConnection();
			PreparedStatement pstmt=con.prepareStatement("select count(_id) from res_messages where _receiver=? and _status=0");
			pstmt.setInt(1, uid);
			ResultSet rs=pstmt.executeQuery();
			while(rs.next()){
				return rs.getInt(1);
			}
			return 0;
		}catch(SQLException e){
			e.printStackTrace();
			return 0;
		}finally{
		    try{
		    	con.close();
		    }catch(Exception e){
		    	
		    }
		}
	}
	public void updateMessage(List<Message> messages){
		Connection con=null;
		try{
			con=ConnectionPool.getPool().getConnection();
			PreparedStatement pstmt=con.prepareStatement("update res_messages set _status=? where _id=?");
			for(Message message:messages){
				pstmt.setInt(1, message.getStatus());
				pstmt.setInt(2, message.getId());
				pstmt.addBatch();
			}
			pstmt.executeBatch();
		}catch(SQLException e){
			e.printStackTrace();
			return;
		}finally{
			try{
				con.close();
			}catch(Exception e){
				
				
			}
		}
	}
}
